<template>
  <div class="review-result">
    <el-button type="primary" v-preventReClick @click="handleExportAll()">
      下载评分汇总表
    </el-button>
    <el-button type="primary" v-preventReClick @click="handleExportDetail()">
      下载评分明细表
    </el-button>
    <el-button type="primary" v-preventReClick @click="handleExportReview()">
      批量导出评审
    </el-button>
    <el-button
      type="primary"
      v-preventReClick
      @click="handleExportAll()"
      v-if="false"
    >
      下载评分记录表
    </el-button>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      @selection-change="handleSelectionChange"
      max-height="600"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column type="expand">
        <template slot-scope="{ row }">
          <p v-if="!row.expertsTaskList.length">暂无评审信息</p>
          <div v-else>
            <el-descriptions
              v-for="v in row.expertsTaskList"
              :key="v.id"
              size="small"
              :column="4"
            >
              <el-descriptions-item label="专家">
                {{ expertName(v) }}
              </el-descriptions-item>
              <el-descriptions-item label="评审状态">
                <span class="color-info" v-show="v.status != 2">待评审</span>
                <span class="color-success" v-show="v.status == 2">已评审</span>
              </el-descriptions-item>
  
              <el-descriptions-item label="总评分">
                {{ totalScore(v.score) }}
              </el-descriptions-item>
              <el-descriptions-item label="操作">
                <el-button-group>
                  <el-button
                    type="success"
                    size="mini"
                    @click="handleReviewHistory(v)"
                    :disabled="v.status != 2"
                  >
                    查看评审
                  </el-button>
                  <el-button
                    type="primary"
                    size="mini"
                    @click="handleExport(v)"
                    :disabled="v.status != 2"
                  >
                    导出评审
                  </el-button>
                </el-button-group>
              </el-descriptions-item>
            </el-descriptions>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="date" label="名称">
        <template slot-scope="{ row }">
          <span>
            {{ row.courseInfo.title }}
          </span>
        </template>
      </el-table-column>

      <el-table-column prop="name" label="板块类别">
        <template slot-scope="{ row }">
          <span>
            {{ row.courseInfo.courseType | auditType }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="建设单位">
        <template slot-scope="{ row }">
          <span>
            {{ deptNames(row.courseInfo) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="总评分">
        <template slot-scope="{ row }">
          <span>
            {{ totalScore(row.resultScore) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button
              type="success"
              size="mini"
              @click="handlePreview(row.courseInfo)"
            >
              预览
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
    <ReviewHistory />
  </div>
</template>
<script>
import coursePreview from '@/mixins/coursePreview'
import {
  expertsTaskQueryResultList,
  openApiExportWordExpCollectZip,
} from '@/api/admin/audit/expertsTask.js'
import Bus from '@/utils/bus.js'
export default {
  mixins: [coursePreview],
  components: {
    ReviewHistory: () =>
      import('@/views/UserCenter/audit/myReview/ReviewHistory.vue'),
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        reviewId: '',
      },
      tableData: [],
      pageObj: {},
      multipleSelection: [],
    }
  },
  props: ['extFiled'],
  computed: {
    deptNames() {
      return (v) => {
        if (v.sourceType == 2 && v.extFiled2) {
          //校外资源前端显示资源建设单位
          return v.extFiled2
        } else if (v.tbSysDept) {
          let dep = v.tbSysDept.deptFullnamepath.split('/')
          if (dep.length >= 3) {
            return dep[2]
          } else {
            return '陆军工程大学'
          }
        } else {
          return '陆军工程大学'
        }
      }
    },
    totalScore() {
      return function (v) {
        return v == '0.0' || v == '0' ? '暂无评分' : v
      }
    },
    expertName() {
      return function (v) {
        let userId = this.$store.getters.userId
        // 组长和创建人看专家名称
        if (userId == v.createBy || userId == v.isLeader) {
          return v.expertsInfo.userName
        } else {
          return '匿名'
        }
      }
    },
  },
  mounted() {
    this.listQuery.reviewId = this.extFiled.reviewId
    this.getList()
  },
  methods: {
    async getList() {
      let res = await expertsTaskQueryResultList(this.listQuery)
      this.tableData = res.data.data
      this.tableData.forEach((v) => {
        this.$set(v, 'courseInfo', v.expertsReviewResource.sysCourseInfo)
      })
      this.pageObj = res.data
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    //查看评审
    handleReviewHistory(v) {
      Bus.$emit('showDialogReviewHistory', v)
    },
    //批量导出
    handleExportReview() {
      if (!this.multipleSelection.length) {
        this.$message({
          message: '请选择资源',
          type: 'warning',
        })
        return
      }
      let resourceIds = this.multipleSelection
        .map((v) => v.expertsTaskList)
        .flat()
        .filter((v) => v.status == 2)
        .map((v) => v.id)
        .join('AND')
      let href =
        this.baseURL + '/openApi/exportWord/expCollectZip/' + resourceIds
      window.location.href = href
    },
    //导出单个评审
    handleExport(v) {
      window.location.href =
        this.baseURL + '/openApi/exportWord/auditDetail?id=' + v.id
    },
    handleExportDetail() {
      if (!this.multipleSelection.length) {
        this.$message({
          message: '请选择资源',
          type: 'warning',
        })
        return
      }
      let resourceIds = this.multipleSelection
        .map((v) => v.expertsReviewResource)
        .map((v) => v.id)
        .join('AND')
      window.location.href =
        this.baseURL +
        '/openApi/exportWord/auditCollect2?resourceIds=' +
        resourceIds +
        '&reviewId=' +
        this.extFiled.reviewId
    },
    //下载自评意见表
    handleExportAll() {
      if (!this.multipleSelection.length) {
        this.$message({
          message: '请选择资源',
          type: 'warning',
        })
        return
      }
      let courseIdArr = []
      let resourceArr = []
      this.multipleSelection.forEach((v) => {
        courseIdArr.push(v.courseInfo.id)
        resourceArr.push(v.expertsReviewResource.id)
      })
      let courseIds = courseIdArr.join('AND')
      let resourceIds = resourceArr.join('AND')
      let currentUserId = this.$store.getters.userId
      let data = {
        status: 2,
        taskType: 2,
        courseIds,
        resourceIds,
        currentUserId,
      }
      window.location.href =
        this.baseURL +
        '/openApi/exportWord/auditCollect' +
        this.json2params(data)
    },
  },
}
</script>
<style scoped >
.review-result >>> .el-descriptions-item {
  line-height: 28px;
}
.review-result >>> .el-table__expand-icon > .el-icon {
  font-weight: bold;
  font-size: 16px;
}
.review-result >>> .el-table td.el-table__cell div {
  font-size: 14px;
}
</style>